package com.css.dzj.dzjjcss.modules.press.service.impl;

import com.css.dzj.dzjjcss.common.util.Constant;
import com.css.dzj.dzjjcss.common.util.SnowflakeId;
import com.css.dzj.dzjjcss.modules.press.entity.Press;
import com.css.dzj.dzjjcss.modules.press.entity.QPress;
import com.css.dzj.dzjjcss.modules.press.mapper.PressMapper;
import com.css.dzj.dzjjcss.modules.press.service.PressService;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
public class PressServiceImpl implements PressService {
    @Autowired
    private PressMapper pressMapper;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Override
    public void add(Press press) {
        press.setId(SnowflakeId.getId());
        press.setCreateTime(new Date());
        press.setDelFlag(Constant.NO_DELETE);
        press.setStatus("0");//催办
        pressMapper.save(press);
    }

    @Override
    public void update(Press press) {
        pressMapper.saveAndFlush(press);
    }

    @Override
    public Press queryById(String id) {
        QPress q = QPress.press;
        return jpaQueryFactory.selectFrom(q)
                .where(q.id.eq(id), q.delFlag.eq(Constant.NO_DELETE)).fetchOne();
    }

    @Transactional
    @Override
    public void logicDel(String id) {
        QPress q = QPress.press;
        jpaQueryFactory.update(q)
                .set(q.delFlag, Constant.DELETE)
                .where(q.id.eq(id))
                .execute();
    }

    @Override
    public void realDel(String id) {
        pressMapper.deleteById(id);
    }
}
